package com.usiful.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.usiful.model.ActionArg;
import com.usiful.utils.FileUtil;
import com.usiful.utils.PathUtil;

/**
 * gradle脚本过滤器
 * 
 * @author [周织能]
 *
 */
public class GradleScriptFilterController extends BaseController {

	public final static String ARG_PROJECT_PATH = "path";

	protected String mProjectPath;
	protected Map<String, Object> mPropertiesMap;
	protected String[] mSupportTypes = { 
			"compileSdkVersion", "buildToolsVersion", "defaultConfig", "compileOptions",
			"buildTypes", "lintOptions", "packagingOptions", "testOptions", "aaptOptions", 
			"dexOptions", "dependencies", "productFlavors" 
			};

	@Override
	public boolean handleAction(ActionArg arg) {
		// TODO Auto-generated method stub
		super.handleAction(arg);
		mProjectPath = arg.getArgValueMap().get(ARG_PROJECT_PATH).get(0);
		try {
			handleEngineJsonOfProject();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * 处理引擎项目中的engine.json
	 * 
	 * @throws Exception
	 */
	public void handleEngineJsonOfProject() throws Exception {
		String engineJsonPath = PathUtil.getFullPath(mProjectPath, "engine.json");
		File engineJsonFile = new File(engineJsonPath);
		if (engineJsonFile.exists()) {
			JSONObject engineJsonObj = new JSONObject(FileUtil.readFile2String(engineJsonPath));
			mPropertiesMap = new HashMap<String, Object>();
			if (engineJsonObj.has("gradle")) {
				JSONObject gradleObj = engineJsonObj.getJSONObject("gradle");
				for(String type : mSupportTypes) {
					if(gradleObj.has(type)) {
						mPropertiesMap.put(type, gradleObj.get(type));
					}
				}
				generateGradleBuild();
			}
		} else {
			throw new Exception("can not find engine.json!");
		}
	}

	public GradleScriptFilterController() {

	}

	protected void generateGradleBuild() {
		String gradleContent = "";
		gradleContent += "buildscript{\n\t";
		gradleContent += "repositories {\n\t\t";
		gradleContent += "mavenCentral()\n\t}\n\t";
		gradleContent += "denpendencies {\n\t\t";
		gradleContent += "classpath \'com.android.tools.build:gradle:0.14.+\'\n\t";
		gradleContent += "}\n";
		gradleContent += "}\n";
		gradleContent += "apply plugin: \'android\'\n";
		gradleContent += "android {\n";
		Set<String> keySet = mPropertiesMap.keySet();
		for (String key : keySet) {
			if (mPropertiesMap.get(key) instanceof JSONArray) {
				gradleContent += "\t" + key + " {\n";
				JSONArray arr = (JSONArray) mPropertiesMap.get(key);
				for (int i = 0; i < arr.length(); i++) {
					try {
						gradleContent += "\t\t" + arr.getString(i) + "\n";
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				gradleContent += "\n\t}\n";
			} else {
				gradleContent += "\t" + key + " " + mPropertiesMap.get(key).toString() + "\n";
			}
		}
		gradleContent += "\tsourceSets {\n\t\t";
		gradleContent += "main {\n\t\t\t";
		gradleContent += "manifest.srcFile \'AndroidManifest.xml\'\n\t\t\t";
		gradleContent += "java.srcDirs = [\'src\']\n\t\t\t";
		gradleContent += "resources.srcDirs = [\'src\']\n\t\t\t";
		gradleContent += "aidl.srcDirs = [\'src\']\n\t\t\t";
		gradleContent += "renderscript.srcDirs = [\'src\']\n\t\t\t";
		gradleContent += "res.srcDirs = [\'res\']\n\t\t\t";
		gradleContent += "assets.srcDirs = [\'assets\']\n\t\t\t";
		gradleContent += "jniLibs.srcDirs = [\'libs\']\n\t\t}\n\t\t";
		gradleContent += "instrumentTest.setRoot(\'tests\')\n\t\t";
		gradleContent += "debug.setRoot(\'build-types/debug\')\n\t\t";
		gradleContent += "release.setRoot(\'build-types/release\')\n\t";
		gradleContent += "}\n\t\t";
		gradleContent += "\n}";
		FileUtil.writeString2File(gradleContent, PathUtil.getFullPath(mProjectPath, "engine.json"));
	}
}
